﻿/**
 * jQuery 页面打印插件
 * http://www.css88.com
 */

(function ($) {
    $.fn.printPage = function (options) {
        var pluginOptions = {
            attr: "href",
            url: false,
            message: "请稍后，真正为您准备文档..."
        };
        $.extend(pluginOptions, options);

        $(this).unbind("click").click(function () {
            PrintPgae.loadPrintDocument(this, pluginOptions); return false;
        })
       // this.live("click", function () { PrintPgae.loadPrintDocument(this, pluginOptions); return false; });

        var PrintPgae = {
            thisMsgBox:null,
            loadPrintDocument: function (el, pluginOptions) {
                var kk = this.creatMsgBox(pluginOptions.message);
                kk.appendTo(parent.document.body)
                .css("opacity", 0).addClass("alert").addClass("alert-block")
                    .animate({ opacity: 1 }, 300, function () {
                        PrintPgae.addIframeToPage(el, pluginOptions);
                        kk.delay(1000).fadeOut(function () { kk.remove();})
                });
            },
            creatMsgBox: function (message) {
                return $("<div>", {
                    id: "printMessageBox",
                    html: pluginOptions.message
                });
            },
            iframe: function (url) {
                return '<iframe id="printPage" name="printPage" src=' + url + ' style="position:absolute;top:0px; left:0px;width:0px; height:0px;border:0px;overfow:none; z-index:-1"></iframe>';
            },
            addIframeToPage: function (el, pluginOptions) {
                var url = pluginOptions.url ? pluginOptions.url : $(el).attr(pluginOptions.attr);
                if (!$("#printPage")[0]) {
                    $("body").append(PrintPgae.iframe(url));
                    $('#printPage').bind("load", function () { PrintPgae.printit(); })
                } else {
                    $('#printPage').attr("src", url);
                }
            },
            //打印
            printit: function () {
                frames["printPage"].focus();//for ie
                frames["printPage"].print();
                this.unloadMessage();
            },
            unloadMessage: function () {
                //this.thisMsgBox.delay(1000).animate({ opacity: 0 }, 700, function () {
                //    $(this).remove();
                //})
                //this.thisMsgBox.remove();
            }
        }

    };
})(jQuery);